{% extends 'base.html' %}
{% block title %}
接口测试
{% endblock %}
{% block content %}
<br>
<div class="btn-group">
    <button type="button" class="btn btn-info">{{ product.name }}</button>
    <button type="button" class="btn btn-primary dropdown-toggle btn-sm" data-toggle="dropdown">
        <span class="caret"></span>
        <span class="sr-only">切换下拉菜单</span>
    </button>
    <ul class="dropdown-menu" role="menu">
        {% for pd in products %}
        <li><a href="{{ url_for('wx.itest.test',pk=pd.id) }}">{{ pd.name }}</a></li>
        {% endfor %}
    </ul>
</div>
<br>
<br>
<div style="float: right">
    <button type="button" class="btn btn-outline-info" data-toggle="modal" data-target="#myModal">添加</button>
</div>
<!-- 模态框 -->
<div class="modal fade" id="myModal">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <!-- 模态框头部 -->
            <div class="modal-header">
                <h4 class="modal-title">新增一个用例</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

            <!-- 模态框主体 -->
            <div class="modal-body">
                <form method="post">
                    <div class="mb-3">
                        <label for="apitestname" class="form-label">流程用例名称<label style="color: red;">*</label></label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="请输入用例名称..." autofocus
                            required>
                        <div id="namehelp" class="form-text">输入用例名称</div>
                    </div>
                    <br>
                    <div class="form-actions well well-sm clearfix">
                        <button type="button" class="btn btn-info" data-dismiss="modal">
                            <i class="fa fa-undo"></i>
                            取消
                        </button>
                        <button type="submit" class="default btn btn-primary hide-xs">
                            <i class="fa fa-save"></i>
                            增加
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<!-- 模态框 -->
<br>
<br>
<div class="table-responsive">
    <table class="table table-bordered table-striped table-sm">
        <thead>
            <tr>
                <th>ID</th>
                <th>流程接口测试名称</th>
                <th>测试步骤管理</th>
                <th>执行人</th>
                <th>所属项目</th>
                <th>执行测试</th>
                <th>测试结果</th>
                <th>更新时间</th>
                <td>最后一次运行任务ID</td>
                <th>定时任务</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% if tests %}
            {% for test in tests %}
            <tr>
                <td><strong>{{ test.id }}
                </strong></td>
                <td><strong>{{ test.name }}</strong></td>
                <td><a href="{{ url_for('wx.itest.step',pk=test.id) }}">
                    进入</a></td>
                <td>{{ test.user }}</td>
                <td class="bg-light text-dark">{{ test.product.name }}</td>
                <td>
                    <button type="button" value="{{ test.id }}" class="testrunner btn-primary btn-sm">
                        运行
                    </button>
                </td>
                {% if test.results == -1 %}
                <td><span class="badge badge-warning">未测试</span></td>
                {% elif test.results == 1 %}
                <td><span class="badge badge-success">成功</span></td>
                {% elif test.results == 0 %}
                <td><span class="badge badge-danger">失败</span></td>
                {% endif %}
                <td>{{ moment(test.updated).format('lll') }}</td>
                <td><small class="text-muted">{% if test.task_id %}
                        <a href="{{ url_for('wx.bp_report.report') }}?task_id={{ test.task_id }}">
                            {{ test.task_id }}</a>
                        {% else %}
                        暂无
                        {% endif %}</small></td>
                <td>
                    <button type="button" class="btn btn-outline-info btn-sm" data-toggle="modal"
                        data-target="#crontab">添加</button>
                </td>
                <div class="modal fade" id="crontab">
                    <div class="modal-dialog modal-lg">
                        <div class="modal-content">

                            <!-- 模态框头部 -->
                            <div class="modal-header">
                                <h4 class="modal-title">新增定时任务</h4>
                                <button type="button" class="close" data-dismiss="modal" id="crons2test"
                                    value="{{ test.id }}">&times;</button>
                            </div>

                            <!-- 模态框主体 -->
                            <div class="modal-body">
                                <form action="{{ url_for('wx.job.add_crontab_test',pk=test.id) }}" method="post">
                                    <div class="mb-3">
                                        <label for="name" class="form-label">任务类型<label
                                                style="color: red;">*</label></label>
                                        <select class="form-control" id="trigger" name="trigger">
                                            {% for cron in crontab %}
                                            <option value="{{ cron[0] }}">{{ cron[1] }}</option>
                                            {% endfor %}
                                        </select>
                                    </div>
                                    <div class="mb-3">
                                        <label for="apitestname" class="form-label">时间间隔<label
                                                style="color: red;">*</label></label>
                                        <input type="text" class="form-control" id="jobsecond" name="jobsecond"
                                            placeholder="请输入时间间隔..." autofocus required>
                                        <div id="namehelp" class="form-text">输入时间间隔</div>
                                    </div>
                                    <div class="panel panel-default">
                                        <div class="panel-heading">
                                            <h4 class="panel-title">
                                                规则说明
                                            </h4>
                                        </div>
                                        <div panel-body>
                                            <div class="form-text">定时任务：   需要按照crontab语法，例:
                                                https://www.runoob.com/w3cnote/linux-crontab-tasks.html</div>
                                            <div class="form-text">一次性任务： 输入格式为: 1990-01-01 00:00:00</div>
                                            <div class="form-text">循环任务：   输入格式为: 数字，单位是秒</div>
                                        </div>
                                    </div>
                                    <br>
                                    <div class="form-actions well well-sm clearfix">
                                        <button type="button" class="btn btn-info" data-dismiss="modal">
                                            <i class="fa fa-undo"></i>
                                            取消
                                        </button>
                                        <button type="submit" class="default btn btn-primary hide-xs">
                                            <i class="fa fa-save"></i>
                                            增加
                                        </button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <td><a href="{{ url_for('wx.itest.edit_test',pk=test.id) }}">编辑</a></td>
            </tr>
            {% endfor %}
            {% else %}
            <p style="color: red">暂无数据</p>
            {% endif %}
        </tbody>
    </table>
</div>
{% endblock %}
{% block script %}
<script>
    $(document).ready(function () {
        $(".testrunner").click(function () {
            var pk = $(this).val()
            console.log("测试用例ID" + pk)
            $.get({
                url: "{{ request.host_url }}jobs/{{ product.id}}/" + pk,
                dataType: 'json',
                success: function (response) {
                    $(this).removeAttr('disabled')
                    $(this).text("运行")
                    alert("任务" + pk + response.errmsg);
                    location.reload();
                }
            });
            $(this).text("运行中")
            $(this).attr("disabled", "disabled")
        });
    });
</script>
{% endblock %}